﻿/*
 * Filehandling.cpp
 *
 * Created: 07-03-2013 13:21:56
 *  Author: Bootie
 */ 

#include "../Headers/Filesystemhandler.h"

Filesystemhandler::Filesystemhandler(BYTE _vol)
{
	vol = _vol;
	bool res;
	memset(&fs, 0, sizeof(fs));
	
	res = Mount();
	if(res != true)
		return;// Write error message
		
	res = CheckFS();
	if(res == true)
		return;// Nothing more to do.
		
	res = CreateFS();
	if(res != true)
		return;// Write error message.	
}

Filesystemhandler::~Filesystemhandler()
{
	
}

bool Filesystemhandler::CheckFS()
{
	FRESULT res = f_opendir(&dir, ROOT_DIRECTORY);
	if(res == FR_NO_FILESYSTEM)
		return false;
	else
		return true;
}

bool Filesystemhandler::CreateFS()
{
	FRESULT res = f_mkfs(vol, 0, 512);
	if(res == FR_OK)
		return true;
	else
		return false;
}

bool Filesystemhandler::Mount()
{
	FRESULT res = f_mount(vol, &fs);
	if(res == FR_OK)
		return true;
	else
		return false;
}

bool Filesystemhandler::Unmount()
{
	FRESULT res = f_mount(vol, NULL);
	if(res == FR_OK)
		return true;
	else
		return false;
}

const TCHAR Filesystemhandler::ROOT_DIRECTORY[] = "/";